package in.muton.edushare.components.tasks.invitations;

import in.muton.edushare.newentities.upgraded.singleInvite.GroupInvite;
import in.muton.edushare.pages.EduShareBasePage;
import in.muton.edushare.pages.common.EdusharePanel;
import in.muton.edushare.wicket.converters.choicerenderers.GenericRenderer;
import in.muton.edushare.wicket.models.CayenneModel;

import java.util.HashMap;
import java.util.List;

import org.apache.wicket.markup.html.CSSPackageResource;
import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.markup.html.form.Button;
import org.apache.wicket.markup.html.form.DropDownChoice;
import org.apache.wicket.markup.html.form.Form;
import org.apache.wicket.model.IModel;

import com.edushare.common.dao.GroupsDAO;
import com.edushare.common.vo.GroupMembers;
import com.edushare.common.vo.Groups;
import com.edushare.common.vo.Users;

public class ThirdPartyGroupInvite extends EdusharePanel {
	private static final long serialVersionUID = -5963364373557131680L;
	private IModel<Users> owner = null;
	private GenericRenderer<Integer> gr;
	private IModel<Groups> groupModel = null;

	public ThirdPartyGroupInvite(String id, IModel<Users> model,
			EduShareBasePage page) {
		super(id, model, page);
		owner = model;
		add(CSSPackageResource.getHeaderContribution(
				ThirdPartyCourseInvite.class, "Invitations.css"));
		Form<Void> f = new Form<Void>("dummyForm");
		add(f);
		f.add(new Label("userName", owner.getObject().getCompleteUserName()));
		f.add(new Button("memberInvite") {
			@Override
			public void onSubmit() {
				getSelectedInst();
				setError(new GroupInvite(groupModel).inviteUsers(
						GroupMembers.GROUP_MEMBER, owner.getObject(),
						getORMInterface()));
			}

			private static final long serialVersionUID = 5089484037645395257L;
		});
		f.add(new Button("adminInvite") {
			@Override
			public void onSubmit() {
				setError(new GroupInvite(groupModel).inviteUsers(
						GroupMembers.GROUP_ADMIN, owner.getObject(),
						getORMInterface()));
			}

			private static final long serialVersionUID = 5089484037645395257L;
		});

		gr = new GenericRenderer<Integer>(getOwnedGroups(), null);
		f.add(new DropDownChoice<String>("deptSelect", gr.getModel(), gr
				.getOptions(), gr));
	}

	private HashMap<String, Integer> getOwnedGroups() {
		List<Groups> groupList = getLoggedInUser().getOwnedGroup();
		HashMap<String, Integer> map = new HashMap<String, Integer>();
		for (Groups group : groupList) {
			if(!(owner.getObject().isGroupAdmin(group) && owner.getObject().isGroupMember(group)))
				map.put(group.getGroupName(), group.getGroupId());
		}
		return map;
	}

	private void getSelectedInst() {
		if (gr.getSelection() == null) {
			setError("Please choose a group to invite "
					+ owner.getObject().getCompleteUserName() + "to");
			return;
		}
		int instId = gr.getSelection();
		Groups group = new GroupsDAO(getORMInterface()).getGroupById(instId);
		groupModel = new CayenneModel<Groups>(group);
	}

	@Override
	public boolean isVisible() {
		return getOwnedGroups().size() > 0
				&& getLoggedInUser() != owner.getObject();
	}
}
